qx.Class.define("caraffa.forms.Cirugias",
{
extend : qx.ui.form.ow.FormTab,
construct : function (fecha,usuario)
{
	this.base(arguments, "Cirugias", "Cirugias");
	this.fecha = fecha;
	//RPC
	this.rpcCir = new qx.io.remote.Rpc();
	this.rpcCir.setTimeout(10000);
	this.rpcCir.setUrl("services/");
	this.rpcCir.setServiceName("cirugias");
	
	
	this.grpCirugias = new qx.ui.groupbox.GroupBox();
		this.grpCirugias.setLayout(new qx.ui.layout.Basic());
		var txdFechaCir = "";
		this.txdFechaCir = txdFechaCir = new qx.ui.form.ow.DateText("Día:");
			this.txdFechaCir.getLabel().setWidth(30);
			this.txdFechaCir.setValue(this.fecha);
		this.cmbClinica = new qx.ui.form.ow.ComboBox("Clinica:", [{value:"1", label:"ZIPOLI	"}, {value:"2", label:"AVELLANEDA"}],true);
			this.cmbClinica.getLabel().setWidth(60);
			this.cmbClinica.getCombo().setWidth(120);
		this.btnBuscarCir = new qx.ui.form.Button("Buscar Cirugias", "caraffa/22/largavista.png");
		this.rdbCanceladas = new qx.ui.form.ow.Radios("Mostrar canceladas:",[{value:'0', label:'No'},{value:'1', label:'Si'}],true); 
		this.mdlCirugias = this.mdlCirugias = new qx.ui.table.model.Simple();
			this.mdlCirugias.setColumns(["cod_turno","N°","Citado","Paciente","Cirugía","Médico", "Obra Social","Observaciones","Ojo","Confirma", "CheckIn", "Cerrado"]);
		this.tblCirugias = new qx.ui.table.Table(this.mdlCirugias);
			this.tblCirugias.setStatusBarVisible(false);
			this.tblCirugias.setColumnVisibilityButtonVisible(true);
			this.tblCirugias.setColumnWidth(0, 50);
				this.tblCirugias.getTableColumnModel().setColumnVisible(0,false);
			this.tblCirugias.setColumnWidth(1, 50);
			this.tblCirugias.setColumnWidth(2, 70);
			this.tblCirugias.setColumnWidth(3, 200);
			this.tblCirugias.setColumnWidth(4, 330);
			this.tblCirugias.setColumnWidth(5, 90);
			this.tblCirugias.setColumnWidth(6, 200);
				this.tblCirugias.getTableColumnModel().setColumnVisible(6,false);
			this.tblCirugias.setColumnWidth(7, 100);
				this.tblCirugias.getTableColumnModel().setColumnVisible(7,false);
			this.tblCirugias.setColumnWidth(8, 50);
				this.tblCirugias.getTableColumnModel().setColumnVisible(8,false);
			this.tblCirugias.setColumnWidth(9, 70);
			this.tblCirugias.setColumnWidth(10, 60);
			this.tblCirugias.setColumnWidth(11, 60);			
			this.tblCirugias.setShowCellFocusIndicator(false);
			this.tblCirugias.setWidth(950);
			this.tblCirugias.setHeight(215);
			
	this.txaDatos = new qx.ui.form.ow.TextArea("");
		this.txaDatos.getText().setReadOnly(true);
		this.txaDatos.getText().setHeight(140);
		this.txaDatos.setWidth(950);
		
	this.grpBotonera = new qx.ui.groupbox.GroupBox();
		this.grpBotonera.setWidth(950);
		this.grpBotonera.setLayout(new qx.ui.layout.HBox(20).set({alignX:"center"}));
		this.btnAgregarCir = new qx.ui.form.Button("Agregar","caraffa/22/aceptar.png");
		this.btnModificarCir = new qx.ui.form.Button("Modificar","caraffa/22/actualizar.png");
		this.btnCancelarCir = new qx.ui.form.Button("Cancelar","caraffa/22/cancelar.png");
		this.btnFormaDePago = new qx.ui.form.Button("CheckIn y Pago","caraffa/22/calculadora.png");
		this.btnCerrarCir = new qx.ui.form.Button("Cerrar","caraffa/22/visto.png");		
		this.btnImpCirOrden = new qx.ui.form.Button("Cirugías","caraffa/22/imprimir.png");
		this.btnImpCirTotal = new qx.ui.form.Button("Cierre","caraffa/22/imprimir.png");
		
	this.grpBotonera.add(this.btnAgregarCir);
	this.grpBotonera.add(this.btnModificarCir);
	this.grpBotonera.add(this.btnCancelarCir);
	this.grpBotonera.add(this.btnFormaDePago);
	this.grpBotonera.add(this.btnCerrarCir);
	this.grpBotonera.add(this.btnImpCirOrden);
	this.grpBotonera.add(this.btnImpCirTotal);
	if(usuario == 1 || usuario == 2 || usuario == 5) this.grpBotonera.setEnabled(true)
	else this.grpBotonera.setEnabled(false);
			
	this.grpCirugias.add(this.txdFechaCir,{top:0,left:0});
	this.grpCirugias.add(this.cmbClinica,{top:0,left:250});
	this.grpCirugias.add(this.rdbCanceladas,{top:0,left:500});
	this.grpCirugias.add(this.btnBuscarCir,{top:0,left:800});
	this.grpCirugias.add(this.tblCirugias,{top:35,left:0});
	this.grpCirugias.add(this.txaDatos,{top:240,left:0});
	this.grpCirugias.add(this.grpBotonera,{top:400,left:0});
	
	this.add(this.grpCirugias,{top:0,left:0});
	this.attachEvents();
},

members: {
	
	attachEvents : function (){
		this.cmbClinica.addListener('changeValue', this.initForm,this);
		this.btnBuscarCir.addListener('execute', this.buscarCir, this);
		this.txdFechaCir.getDateText().addListener('changeValue', this.getCirugias, this);
		this.rdbCanceladas.addListener('changeValue', this.getCirugias, this);
		this.btnFormaDePago.addListener('execute', this.formaDePago, this);
		this.btnImpCirOrden.addListener('execute', this.printCirOrden, this);
		this.btnImpCirTotal.addListener('execute', this.printCirTotal, this);
		this.btnAgregarCir.addListener("execute",this.agregarCirugia,this);
		this.btnModificarCir.addListener("execute",this.modificarCirugia,this);
		this.btnCancelarCir.addListener("execute",this.cancelarCirugia,this);
		this.btnCerrarCir.addListener("execute",this.cerrarCirugia,this);
		this.tblCirugias.addListener('cellClick', this.asignarDatos, this);
		this.tblCirugias.addListener("keyup", function (e)
		{
			if ((e.getKeyIdentifier() == 'Up') || (e.getKeyIdentifier() == 'Down'))
			{
				this.asignarDatos();
			}
		}, this);
		
	},
	initForm : function(){
		this.getCirugias();
	},
	getCirugias: function(){
		this.txaDatos.setValue("");
		try{
			var params = new Object();
				params.fecha_asignada = this.txdFechaCir.getValueToSQL();
				params.canceladas = this.rdbCanceladas.getValue();
				params.cod_clinica = this.cmbClinica.getValue();
			var resCirugias = this.rpcCir.callSync("getCirugias", params);
			if(resCirugias.values != null){
				this.tblCirugias.getTableModel().setData(resCirugias.values);
			}
			else{
				alert("No hay cirugias en el día seleccionado");
			}
		}
		catch (ex){
			alert(ex);
		}
		
	},
	formaDePago:function(){
		if(this.mdlCirugias.getValue(9, this.tblCirugias.getFocusedRow())!= "C"){
			var cod_turno = this.mdlCirugias.getValue(0, this.tblCirugias.getFocusedRow());
			var winFormaDePago = new caraffa.widgets.ABMFormaPago(cod_turno, "C");
				winFormaDePago.moveTo(30, 130);
				winFormaDePago.setModal(true);
				winFormaDePago.addListener("close", this.getCirugias,this);
  				winFormaDePago.open();
		}
		else{
			alert("Cirugia cancelada, no se cobrará.");
		}
	},
	printCirOrden:function(){
			var params = new Object();
				params.fecha = this.txdFechaCir.getValueToSQL();
			var print = this.rpcCir.callSync("printCirOrden", params);
			window.open(print);	
	},
	printCirTotal:function(){
		var params = new Object();
				params.fecha = this.txdFechaCir.getValueToSQL();
			var print = this.rpcCir.callSync("printCirTotal", params);
			window.open(print);	
	},	
	agregarCirugia:function(){
		var winAgregar = new caraffa.widgets.TurnoCirugia("Turno Para Cirugía", this.txdFechaCir.getValue(), false);
			winAgregar.addListener('turnoCargado', this.getCirugias, this);
			winAgregar.moveTo(20, 80);
			winAgregar.setModal(true);
  			winAgregar.open();
	},
	modificarCirugia:function(){
		var turno = this.mdlCirugias.getValue(0, this.tblCirugias.getFocusedRow());
		var winAgregar = new caraffa.widgets.TurnoCirugia("Turno Para Cirugía","",turno);
			winAgregar.addListener('turnoCargado', this.getCirugias, this);
			winAgregar.moveTo(20, 80);
			winAgregar.setModal(true);
  			winAgregar.open();
	},	
	cancelarCirugia:function(){
		if(confirm("Cancelar cirugia?")){
			try{
				var params = new Object();
					params.cod_turno = this.mdlCirugias.getValue(0, this.tblCirugias.getFocusedRow());
				var resCancelarCirugia = this.rpcCir.callSync("cancelarTurno", params);
				this.getCirugias();
			}
			catch (ex){
				alert(ex);
			}
		}
	},	
	cerrarCirugia:function(){
		if(confirm("Cerrar cirugia?")){
			try{
				var params = new Object();
					params.cod_turno = this.mdlCirugias.getValue(0, this.tblCirugias.getFocusedRow());
					params.observaciones =" - OJO: " + this.mdlCirugias.getValue(7, this.tblCirugias.getFocusedRow());
					params.observaciones +=" - OBSERVACIONES: " + this.mdlCirugias.getValue(8, this.tblCirugias.getFocusedRow());
					params.fecha = this.txdFechaCir.getValueToSQL();
					params.hora = "00:00";
					
				var resCerrarCirugia = this.rpcCir.callSync("cerrarTurno", params);
				this.getCirugias();
			}
			catch (ex){
				alert(ex);
			}
		}
	},		
	asignarDatos:function(){
		var datosCirugia = "- Cirugía: "+ this.mdlCirugias.getValue(4, this.tblCirugias.getFocusedRow());
			datosCirugia +="\n- Ojo: "+ this.mdlCirugias.getValue(7, this.tblCirugias.getFocusedRow());
			datosCirugia +="\n- Observaciones: "+ this.mdlCirugias.getValue(8, this.tblCirugias.getFocusedRow());
			datosCirugia +="\n------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------";
			datosCirugia +="\n- Obra Social: "+ this.mdlCirugias.getValue(6, this.tblCirugias.getFocusedRow());
		this.txaDatos.setValue(datosCirugia);
		if(this.mdlCirugias.getValue(11, this.tblCirugias.getFocusedRow())== 'S'){
			this.btnCerrarCir.setEnabled(false);
			this.btnCancelarCir.setEnabled(false);
			this.btnModificarCir.setEnabled(false);
			this.btnFormaDePago.setEnabled(false);
		}
		else{
			this.btnCerrarCir.setEnabled(true);
			this.btnCancelarCir.setEnabled(true);
			this.btnModificarCir.setEnabled(true);
			this.btnFormaDePago.setEnabled(true);
		}
	},
	buscarCir:function(){
		this.winAgregar = new caraffa.widgets.CirugiasBuscar("Buscar cirugias", this.txdFechaCir, false);
			this.winAgregar.addListener('cirugiaFinded', this.getCirugias, this);
			this.winAgregar.moveTo(20, 80);
			this.winAgregar.setModal(true);
  			this.winAgregar.open();
	}	
}
});
